﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for D_bonus
/// </summary>
/// 
namespace Bestlife.ERP.DataAccess
{
    public class D_bonus
    {
        SqlDataReader rdRStatus;
        public D_bonus()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Boolean Bonus_Add(Entities.E_Bonus E_eb)
        {
            bool ExProduct = false;
        
            try
            {
                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                //store bv n Pvs must insert
                String insertBonus = "INSERT INTO Bonus(User_id,Tpv,ppv,Cpv,T_sales,T_commision,T_Lship,Subdealers_Id,Bv,user_Status,Lship_Status,Tbv,Store_Lship,Store_comm,Store_bv,Store_pv)VALUES('" + E_eb.User_id + "','" + E_eb.Tpv + "','" + E_eb.Ppv + "','" + E_eb.Cpv + "','" + E_eb.T_sales + "','" + E_eb.T_commision + "','" + E_eb.T_Lship + "','" + E_eb.Subdealers_Id + "','" + E_eb.Bv + "','" + E_eb.User_Status + "','" + E_eb.Lship_Status + "','" + E_eb.Tbv + "','" + E_eb.Store_Lship + "','" + E_eb.Storecomm + "','" + E_eb.Storebv + "','" + E_eb.Storepv + "')";
                ExProduct = Bestlife.ERP.DataAccess.clsCon.AddEditDel(insertBonus);


            }
            catch (Exception ex) 
            {
            
            }
            finally
            {
                clsCon.conn_new.Close();

            }   //finally

            return ExProduct;
        }


        

        public  Bestlife.ERP.Entities.E_Bonus Getbonus(String uid)
        {
            Entities.E_Bonus Getbns = new Entities.E_Bonus();
            try
            {//try

                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);


                String sqlstatus = "SELECT * from Bonus where User_id='" + uid + "'";
               
                rdRStatus = clsCon.ReadTable(sqlstatus);


                if (rdRStatus.Read())
                {
                    Getbns.Tpv = decimal.Parse(rdRStatus["Tpv"].ToString());
                    Getbns.Ppv = decimal.Parse(rdRStatus["ppv"].ToString());
                    Getbns.Cpv = decimal.Parse(rdRStatus["Cpv"].ToString());
                    Getbns.T_sales = decimal.Parse(rdRStatus["T_sales"].ToString());
                    Getbns.T_commision = decimal.Parse(rdRStatus["T_commision"].ToString());
                    Getbns.Subdealers_Id = rdRStatus["Subdealers_Id"].ToString();
                    Getbns.Bv = decimal.Parse(rdRStatus["Bv"].ToString());
                    Getbns.User_Status = rdRStatus["user_Status"].ToString();
                    Getbns.T_Lship = decimal.Parse(rdRStatus["T_Lship"].ToString());
                    Getbns.Lship_Status = decimal.Parse(rdRStatus["Lship_Status"].ToString());
                    Getbns.Tbv = decimal.Parse(rdRStatus["Tbv"].ToString());
                    Getbns.Storepv = decimal.Parse(rdRStatus["Store_pv"].ToString());
                    Getbns.Storepv = decimal.Parse(rdRStatus["Store_bv"].ToString());
                    Getbns.Storecomm = double.Parse(rdRStatus["Store_comm"].ToString());
                    Getbns.CPV_month = decimal.Parse(rdRStatus["CPV_month"].ToString());
                    Getbns.Tppv_month = decimal.Parse(rdRStatus["tppv_month"].ToString());
                    Getbns.Tbv_month = decimal.Parse(rdRStatus["tbv_month"].ToString());
                    Getbns.Store_Lship = decimal.Parse(rdRStatus["Store_Lship"].ToString());
                }


            }//try


            catch (Exception err)
            {
              
            }

            finally
            {

                clsCon.conn_new.Close();
                rdRStatus.Close();
            }   //finally
            return Getbns;



        }


        #region Update monthly commision if chaid not complete his Pv

        public Boolean UpdatemonthlyUp(String id, Decimal TotalCommitions)
        {
            Boolean upd2 = false;
            try
            {
                DataAccess.D_bonus dbns = new D_bonus();
                Entities.E_Bonus ebns = new Entities.E_Bonus();

                ebns = dbns.Getbonus(id);

                Decimal All_commi = decimal.Parse(ebns.T_commision.ToString()) + TotalCommitions;


                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                String updtxt2 = "UPDATE Bonus SET  T_commision ='" + All_commi + "',Last_Modify='" + System.DateTime.Now + "' WHERE User_id='" + id + "'";
                // String updtxt2 = "UPDATE Bonus  SET  T_commision='999' WHERE User_id='001'";
                upd2 = clsCon.AddEditDel(updtxt2);

            }

            catch (Exception err)
            {


            }

            finally
            {
                clsCon.conn_new.Close();

            }

            return upd2;
        }

        #endregion

    }
}